/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobilevikings.app.api;

import com.mobilevikings.util.Utils;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author Jeroen
 */
public class ApiCall {

    private Hashtable args;
    private String methodName;
    private String format;

    /**
     * Creates a new ApiCall.
     * @param methodName the name of the API-call
     */
    public ApiCall(String methodName) {
        this.methodName = methodName;
        args = new Hashtable();
    }

    /**
     * Adds an argument to the API-call
     * @param argKey the name of the argument
     * @param argValue the value of the argument
     */
    public void setArg(String argKey, String argValue) {
        getArgs().put(argKey, argValue);
    }

    /**
     * Checks if an argument is added to the API-call
     * @param inArgName the argument
     * @return true if it is added
     */
    public boolean hasArg(String inArgName) {
        return getArgs().containsKey(inArgName);
    }

    /**
     * Returns a String with all the arguments
     * @return a String with all the arguments
     */
    public String getArgString() {
        String result = "";
        if (!args.isEmpty()) {
            result = "&";
            Enumeration e = args.keys();
            while (e.hasMoreElements()) {
                if (result.length() > 0) {
                    result += "&";
                }
                String argName = (String) e.nextElement();
                //System.out.println("     getArgString - "+argName);
                //result += Utils.urlEncode(argName) + "=" + ((String) args.get(argName));
                result += Utils.urlEncode(argName) + "=" + Utils.urlEncode((String) args.get(argName));
            }
        }

        return result;
    }

    /**
     * Returns a Hashtable with the arguments
     * @return a Hashtable with the arguments
     */
    public Hashtable getArgs() {
        return args;
    }

    public String getMethodName() {
        return methodName;
    }
}
